package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Range;
import com.itheima.domain.mongo.*;
import com.itheima.service.mongo.MovementService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class MovementServiceImpl implements MovementService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisIdService redisIdService;

    @Autowired
    private MovementAsyncService movementAsyncService;
    @Override
    public void publishMovement(Movement movement) {
        //主键自增
        Long pid = redisIdService.getNextId(ConstantUtil.MOVEMENT_ID);
        movement.setPid(pid);
        //保存动态详情
        mongoTemplate.save(movement);
        //2.保存个人动态
        //2-1 封装实体
        MyMovement myMovement = new MyMovement();
        myMovement.setPublishId(movement.getId());
        myMovement.setCreated(movement.getCreated());
        //2-2 mongo 保存
        mongoTemplate.save(myMovement, ConstantUtil.MOVEMENT_MINE + movement.getUserId());

        //调用多线程技术
        movementAsyncService.saveMovementFriend(movement);

    }

    @Override
    public PageBeanVo findMyMovementByPage(Long userid, Integer pageNum, Integer pageSize) {
        //1.构建查询条件
        Integer index = (pageNum - 1) * pageSize;
        Query query = new Query().with(Sort.by(Sort.Order.desc("created"))).skip(index).limit(pageSize);
        //2.查询个人动态表

        List<Movement> list = new ArrayList<>();
        List<MyMovement> myMovements = mongoTemplate.find(query, MyMovement.class, ConstantUtil.MOVEMENT_MINE + userid);
        for (MyMovement myMovement : myMovements) {
            Movement movement = mongoTemplate.findById(myMovement.getPublishId(), Movement.class);
            if (movement.getState() == 1) {
                list.add(movement);
            }
        }
        long count = mongoTemplate.count(new Query(), ConstantUtil.MOVEMENT_MINE + userid);

        return new PageBeanVo(pageNum, pageSize, count, list);
    }

    @Override
    public PageBeanVo findFriendMovementByPage(Long userId, Integer pageNum, Integer pageSize) {

        Integer index = (pageNum - 1) * pageSize;
        Query query = new Query().with(Sort.by(Sort.Order.desc("created"))).skip(index).limit(pageSize);

        List<FriendMovement> friendMovements = mongoTemplate.find(query, FriendMovement.class, ConstantUtil.MOVEMENT_FRIEND + userId);

        List<Movement> lsit = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(friendMovements)) {

            for (FriendMovement friendMovement : friendMovements) {
                Movement movement = mongoTemplate.findById(friendMovement.getPublishId(), Movement.class);
                if (movement.getState() == 1) {
                    lsit.add(movement);
                }
            }
        }
        return new PageBeanVo(pageNum, pageSize, 0L, lsit);
    }

    @Override
    public PageBeanVo findRecommendMovementByPage(Long userId, Integer pageNum, Integer pageSize) {

        Integer index = (pageNum - 1) * pageSize;
        Query query = new Query(
                Criteria.where("userId").is(userId)
        ).with(Sort.by(Sort.Order.desc("score"))).with(Sort.by(Sort.Order.desc("created"))).skip(index).limit(pageSize);
        List<RecommendMovement> recommendMovements = mongoTemplate.find(query, RecommendMovement.class);
        List<Movement> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(recommendMovements)) {

            for (RecommendMovement recommendMovement : recommendMovements) {
                Movement movement = mongoTemplate.findById(recommendMovement.getPublishId(), Movement.class);
                if (movement.getState() == 1) {
                    list.add(movement);
                }

            }
        }
        return new PageBeanVo(pageNum, pageSize, 0L, list);

    }

    @Override
    public Movement findById(ObjectId id) {
        return mongoTemplate.findById(id, Movement.class);
    }

    // 后台动态查询
    @Override
    public PageBeanVo findMovementByPage(Long userId, Integer state, Integer pageNum, Integer pageSize) {
        Query query = new Query().with(Sort.by(Sort.Order.desc("created"))).skip((pageNum - 1) * pageSize).limit(pageSize);

        if (userId != null) {
            query.addCriteria(Criteria.where("userId").is(userId));
        }

        if (state != null) {
            query.addCriteria(Criteria.where("state").is(state));
        }

        List<Movement> movements = mongoTemplate.find(query, Movement.class);

        Query query1 = new Query().with(Sort.by(Sort.Order.desc("created")));
        if (userId != null) {
            query1.addCriteria(Criteria.where("userId").is(userId));
        }
        if (state != null) {
            query1.addCriteria(Criteria.where("state").is(state));
        }
        long counts = mongoTemplate.count(query1, Movement.class);
        return new PageBeanVo(pageNum, pageSize, counts, movements);

    }

    @Override
    public void updateMovement(Movement movement) {
        mongoTemplate.save(movement);
    }
  /* // 后台动态查询
   @Override
   public PageBeanVo findMovementByPage(Long userId, Integer state, Integer pageNum, Integer pageSize) {
       // 1.构建条件
       Query query = new Query()
               .with(Sort.by(Sort.Order.desc("created")))
               .skip((pageNum-1)*pageSize).limit(pageSize);
       // 动态条件 userId
       if (userId!=null) {
           query.addCriteria(Criteria.where("userId").is(userId));
       }
       // 动态state
       if (state!=null) {
           query.addCriteria(Criteria.where("state").is(state));
       }
       // 2.查询list
       List<Movement> movementList = mongoTemplate.find(query, Movement.class);
       // 3.查询counts
       Query query2 = new Query()
               .with(Sort.by(Sort.Order.desc("created")));
       // 动态条件 userId
       if (userId!=null) {
           query2.addCriteria(Criteria.where("userId").is(userId));
       }
       // 动态state
       if (state!=null) {
           query2.addCriteria(Criteria.where("state").is(state));
       }
       long counts = mongoTemplate.count(query2, Movement.class);
       // 4.封装并返回分页对象
       return new PageBeanVo(pageNum, pageSize, counts, movementList);
   }*/
}
